package com.typy.www.tpay.ui.itemview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.typy.www.tpay.R;
import com.typy.www.tpay.bean.bean.BaseReturn;
import com.typy.www.tpay.bean.vm.ActivicationVm;
import com.typy.www.tpay.bean.vm.NoticeVm;
import com.typy.www.tpay.constance.Constance;
import com.typy.www.tpay.ui.acitivity.WebActivity;
import com.typy.www.tpay.util.ClassHoldUtil;
import com.typy.www.tpay.util.Logutil;
import com.typy.www.tpay.util.ParamsUtil;
import com.typy.www.tpay.util.UpdataUiUtil;
import com.typy.www.tpay.volley.HTTPUtils;
import com.typy.www.tpay.volley.VolleyListener;
import com.zhy.android.percent.support.PercentLinearLayout;

import org.json.JSONException;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 13799 on 2018/6/28.
 */

public class RecordItemView {
    View mView;
    ViewHolder holder;
    private Context mContext;

    public RecordItemView(Context context) {
        mContext = context;
        mView = LayoutInflater.from(context).inflate(R.layout.item_record, null);
        holder = new ViewHolder(mView);
    }

    public View getView() {
        return mView;
    }

    public void bind(final ActivicationVm vm, final int position, final int classType) {
        if (vm != null) {
            if (classType == ClassHoldUtil.AmountListRecord)
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvYewuleixing)
                        .setText(vm.getFdAmount(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
            else if (classType == ClassHoldUtil.SharePointRecord)
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvYewuleixing)
                        .setText(vm.getFdSharePoint(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
            else if (classType == ClassHoldUtil.PointRecord)
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvYewuleixing)
                        .setText(vm.getFdPoint(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
            else if (classType == ClassHoldUtil.ICO) {
                UpdataUiUtil.getDefaul().setText(vm.getFdCount(), holder.tvYewuleixing)
                        .setText("¥" + vm.getFdPrice(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
                holder.tvYewuleixing.setTextColor(mContext.getResources().getColor(R.color.text_moneyred));
                holder.tvShue.setTextColor(mContext.getResources().getColor(R.color.text_moneyred));
            } else if (classType == ClassHoldUtil.ExchangePointRecord) {
                UpdataUiUtil.getDefaul().setText(vm.getFdAmount(), holder.tvYewuleixing)
                        .setText(vm.getFdPoint(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
                holder.tvYewuleixing.setTextColor(mContext.getResources().getColor(R.color.text_moneyred));
                holder.tvShue.setTextColor(mContext.getResources().getColor(R.color.text_moneyred));
            }else if (classType == ClassHoldUtil.ZBAOCENTER) {
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvYewuleixing)
                        .setText(vm.getFdQuantity(), holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
            }else if (classType == ClassHoldUtil.ZBAOSHOUYI) {
                UpdataUiUtil.getDefaul().setText(vm.getFdTypeName(), holder.tvYewuleixing)
                        .setText(vm.getFdAmount()+"", holder.tvShue).setText(vm.getFdCreateTime(), holder.tvShijian);
            }else if (classType == ClassHoldUtil.SELLOUTRECORD) {
                UpdataUiUtil.getDefaul().setText(vm.getFdAmount(), holder.tvYewuleixing)
                        .setText(vm.getFdMoney(), holder.tvShue).setText(vm.getFdSurePayTime(), holder.tvShijian);
            }else if (classType == ClassHoldUtil.BUYINCORECORD) {
                UpdataUiUtil.getDefaul().setText(vm.getFdAmount(), holder.tvYewuleixing)
                        .setText(vm.getFdUserId(), holder.tvShue).setText(vm.getFdSurePayTime(), holder.tvShijian);
            }
        }
        holder.llRoot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(classType == ClassHoldUtil.BUYINCORECORD||classType == ClassHoldUtil.SELLOUTRECORD){
                    getOrderdetailurlURL(vm.getFdOrderId());
                }else{
                    WebActivity.show(mContext, "详情记录", vm.getFdUrl());
                }
            }
        });
    }
    private void getOrderdetailurlURL(String orderId) {
        ParamsUtil params = new ParamsUtil();
        params.put("fdOrderId", orderId);
        HTTPUtils.post(mContext, Constance.getOrderdetailurlURL(), params.getParams(), new VolleyListener() {
            @Override
            public void onResponse(BaseReturn ret) {
                try {
                    String detailUrl = ret.getData().getString("data");
                    WebActivity.show(mContext,"订单详情",detailUrl);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onResponseErr(int code, String msg) {
            }
        });
    }
    static class ViewHolder {
        @BindView(R.id.tv_yewuleixing)
        TextView tvYewuleixing;
        @BindView(R.id.tv_shue)
        TextView tvShue;
        @BindView(R.id.tv_shijian)
        TextView tvShijian;
        @BindView(R.id.ll_root)
        PercentLinearLayout llRoot;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
